Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@peculiar/webcrypto

Package Overview
Dependencies
Maintainers
6
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@peculiar/webcrypto

A WebCrypto Polyfill for NodeJS

  • 1.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.1M
increased by2.66%
Maintainers
6
Weekly downloads
 
Created

What is @peculiar/webcrypto?

The @peculiar/webcrypto package is an implementation of the Web Cryptography API for Node.js. It provides cryptographic operations in web applications, including hashing, signature generation and verification, encryption and decryption, and key generation and management. This package is particularly useful for server-side applications that require cryptographic operations consistent with those available in the browser.

What are @peculiar/webcrypto's main functionalities?

Generating cryptographic keys

This code sample demonstrates how to generate a cryptographic key pair using RSA-PSS algorithm. The generated keys can be used for signing and verification purposes.

const { Crypto } = require('@peculiar/webcrypto');
const crypto = new Crypto();

async function generateKey() {
  const key = await crypto.subtle.generateKey(
    {
      name: 'RSA-PSS',
      modulusLength: 2048,
      publicExponent: new Uint8Array([1, 0, 1]),
      hash: 'SHA-256',
    },
    true,
    ['sign', 'verify']
  );
  return key;
}

Encrypting and decrypting data

This code sample shows how to encrypt data using a public key and decrypt it using a private key with the RSA-OAEP algorithm. It's useful for secure data transmission.

const { Crypto } = require('@peculiar/webcrypto');
const crypto = new Crypto();

async function encryptData(publicKey, data) {
  const encryptedData = await crypto.subtle.encrypt(
    {
      name: 'RSA-OAEP'
    },
    publicKey,
    data
  );
  return encryptedData;
}

async function decryptData(privateKey, encryptedData) {
  const decryptedData = await crypto.subtle.decrypt(
    {
      name: 'RSA-OAEP'
    },
    privateKey,
    encryptedData
  );
  return decryptedData;
}

Signing and verifying data

This example demonstrates signing data with a private key and verifying the signature with the corresponding public key using the RSA-PSS algorithm. It's essential for ensuring data integrity and authenticity.

const { Crypto } = require('@peculiar/webcrypto');
const crypto = new Crypto();

async function signData(privateKey, data) {
  const signature = await crypto.subtle.sign(
    {
      name: 'RSA-PSS',
      saltLength: 32,
    },
    privateKey,
    data
  );
  return signature;
}

async function verifySignature(publicKey, signature, data) {
  const isVerified = await crypto.subtle.verify(
    {
      name: 'RSA-PSS',
      saltLength: 32,
    },
    publicKey,
    signature,
    data
  );
  return isVerified;
}

Other packages similar to @peculiar/webcrypto

Keywords

FAQs

Package last updated on 28 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc